<template>
  <div class="task-management-container">
    <!-- 头部标题和新增按钮 -->
    <div class="header">
      <span class="title">信息管理</span>
      <el-button type="primary" @click="showAddDialog">新增推送任务</el-button>
    </div>

    <!-- 查询条件卡片 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="任务名称：">
          <el-input
              v-model="searchForm.name"
              placeholder="请输入任务名称"
              clearable
              @keydown.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item label="启用状态" style="width:290px;">
          <el-select placeholder="请选择启用状态" v-model="searchForm.status" @change="loadCategories">
            <el-option label="全部" value="" />
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 任务列表表格 -->
    <el-card class="table-card">
      <el-table :data="taskList" stripe border>
        <el-table-column
            prop="id"
            label="序号"
            width="100"
            align="center"
        />
        <el-table-column
            prop="name"
            label="任务名称"
            align="center"
        />
        <el-table-column
            prop="conFunction"
            label="Con函数式"
            align="center"
            show-overflow-tooltip
        />
        <el-table-column
            prop="functionStatus"
            label="推送状态"
            align="center"
        >
          <template #default="{ row }">
            <el-tag>{{ row.functionStatus || '聊天' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="status"
            label="启用状态"
            align="center"
            width="120"
        >
          <template #default="{ row }">
            <el-switch
                v-model="row.status"
                active-value="1"
                inactive-value="0"
                @change="handleStatusChange(row)"
            />
          </template>
        </el-table-column>
        <el-table-column
            label="操作"
            align="center"
            width="200"
        >
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
          class="pagination"
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
      />
    </el-card>

    <!-- 新增/编辑任务对话框 -->
    <el-dialog
        :title="dialogTitle"
        v-model="dialogVisible"
        width="50%"
        :before-close="handleClose"
    >
      <el-form
          :model="taskForm"
          label-width="120px"
          label-position="top"
          ref="taskFormRef"
      >
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="任务名称" prop="name" required>
              <el-input v-model="taskForm.name" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="请选择模板" prop="template">
              <el-select
                  v-model="taskForm.template"
                  placeholder="请选择模板"
                  style="width:100%"
              >
                <el-option label="模板1" value="template1" />
                <el-option label="模板2" value="template2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="Con函数式" prop="conFunction" required>
              <el-input
                  v-model="taskForm.conFunction"
                  placeholder="请输入cron表达式，如：0 0 0 8 7"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="taskForm.status">
                <el-radio border label="1">启用</el-radio>
                <el-radio border label="0">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submitTask">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  status: ''
})

// 任务列表数据
const taskList = ref([
  {
    id: '127',
    name: '女情节',
    conFunction: '0 0 0 8 7',
    functionStatus: '聊天',
    appStatus: '',
    status: '1'
  },
  {
    id: '136',
    name: '双十一',
    conFunction: '0 0 0 1 2 2',
    functionStatus: '聊天',
    appStatus: '',
    status: '1'
  },
  {
    id: '155',
    name: '双十二',
    conFunction: '0 0 0 1 6 7',
    functionStatus: '聊天',
    appStatus: '',
    status: '0'
  },
  {
    id: '124',
    name: '双十一',
    conFunction: '0 0 0 1 1 1 7',
    functionStatus: '聊天',
    appStatus: '',
    status: '1'
  },
  {
    id: '123',
    name: '性别',
    conFunction: '0 0 0 5 * * *',
    functionStatus: '聊天',
    appStatus: '',
    status: '0'
  }
])

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 5
})

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('新增推送任务')
const taskForm = reactive({
  id: '',
  name: '',
  template: '',
  conFunction: '',
  status: '1'
})
const taskFormRef = ref(null)

// 状态变更
const handleStatusChange = (row) => {
  console.log(`任务 ${row.name} 状态变更为: ${row.status === '1' ? '启用' : '禁用'}`)
  // 这里可以添加API调用
  ElMessage.success('状态更新成功')
}

// 搜索
const handleSearch = () => {
  pagination.currentPage = 1
  // 这里可以添加API调用
  console.log('搜索条件:', searchForm)
}

// 重置搜索
const resetSearch = () => {
  searchForm.name = ''
  searchForm.status = ''
  handleSearch()
}

// 显示新增对话框
const showAddDialog = () => {
  dialogTitle.value = '新增推送任务'
  Object.assign(taskForm, {
    id: '',
    name: '',
    template: '',
    conFunction: '',
    status: '1'
  })
  dialogVisible.value = true
}

// 编辑任务
const handleEdit = (row) => {
  dialogTitle.value = '编辑推送任务'
  Object.assign(taskForm, {
    id: row.id,
    name: row.name,
    template: '',
    conFunction: row.conFunction,
    status: row.status
  })
  dialogVisible.value = true
}

// 删除任务
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除任务 "${row.name}" 吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 这里添加删除API调用
    taskList.value = taskList.value.filter(item => item.id !== row.id)
    pagination.total -= 1
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 提交任务表单
const submitTask = () => {
  taskFormRef.value.validate((valid) => {
    if (!valid) return

    if (dialogTitle.value === '新增推送任务') {
      // 新增逻辑
      const newTask = {
        id: Date.now().toString(),
        name: taskForm.name,
        conFunction: taskForm.conFunction,
        functionStatus: '聊天',
        appStatus: '',
        status: taskForm.status
      }
      taskList.value.unshift(newTask)
      pagination.total += 1
      ElMessage.success('新增成功')
    } else {
      // 编辑逻辑
      const index = taskList.value.findIndex(item => item.id === taskForm.id)
      if (index !== -1) {
        taskList.value[index].name = taskForm.name
        taskList.value[index].conFunction = taskForm.conFunction
        taskList.value[index].status = taskForm.status
        ElMessage.success('修改成功')
      }
    }
    dialogVisible.value = false
  })
}

// 关闭对话框
const handleClose = () => {
  taskFormRef.value?.resetFields()
  dialogVisible.value = false
}

// 分页大小变化
const handleSizeChange = (val) => {
  pagination.pageSize = val
  // 这里可以添加数据重新加载逻辑
}

// 页码变化
const handlePageChange = (val) => {
  pagination.currentPage = val
  // 这里可以添加数据重新加载逻辑
}
</script>

<style scoped>
.task-management-container {
  padding: 20px;
  background-color: #f5f7fa;
  height: 100%;
}

.header {
  height: 60px;
  background-color: #fff;
  padding: 10px 20px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header .title {
  font-size: 20px;
  line-height: 40px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>